package com.aau.edu.aio;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

//客户端代码
public class Client {
    public static void main(String[] args) {
        BufferedWriter out = null;//字符输出流
        Socket socket = null;//创建用于通信的socket对象
        try {
            socket = new Socket("127.0.0.1", 8888);//使用构造方法初始化套接字，并绑定服务器IP和端口号
            out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            //初始化字符输出流，使其与socket关联起来，将文本转换为字符流再转换为字节流，通过socket进行发送
            out.write("有朋至远方来");//将文本装换为字符流发送
            out.newLine();//发送换行符
            out.flush();//刷新缓冲区
            if (null != socket)
                socket.close();//关闭套接字
            if (null != out)
                out.close();//关闭字符输出流
        } catch (IOException e) {//异常处理
            throw new RuntimeException(e);
        }
    }
}
